/*
 * BitManager.cpp
 *
 *  Created on: 27/11/2009
 *      Author: ngonzalez
 */

#include "BitManager.h"

BitManager::BitManager() {

	this->stateControl = new StateControl();

}

BitManager::~BitManager() {
	delete this->stateControl;
}

void BitManager::normalize(unsigned long int floor, unsigned long int roof){

	cout<<"normalized"<<endl;
	this->stateControl->setPiso(floor);
	this->stateControl->setTecho(roof);
	this->stateControl->asignar();
	this->stateControl->controlOverflow();
	this->updateAfterOV();
	this->stateControl->controlUnderflow();
	this->updateAfterUF();

}

string BitManager::getCompressedString(){
	this->stateControl->agregarPading();
	return this->stateControl->getEmision();
}

void BitManager::setStringToDecompress(string compressedString) {

}

void BitManager::getInteger() {
	return integer;
}

unsigned int BitManager::getNormalizedFloor(){
	return floor;
}

unsigned int BitManager::getNormalizedRoof(){
	return roof;
}

void BitManager::updateAfterOV () {
	cout<<"updateAfterOV"<<endl;
unsigned int i;
unsigned int contadorOV= this->stateControl->getContadorOV();


unsigned int piso=this->stateControl->getPiso();
unsigned int techo= this->stateControl->getTecho();
cout <<"piso en el update "<<piso<<endl;
cout << "techo en el update "<<techo<<endl;
cout << "contadorOV en updat " <<contadorOV<<endl;
for (i=0; i<contadorOV; i++) {
	piso = (piso<<1) +0 ;
	techo = (techo<<1) +1;
	cout<<i<<" ";
	this->stateControl->mostrarBinario(piso,"pisoshift");
	this->stateControl->mostrarBinario(techo,"techo shift");
}

this->floor=piso;
this->roof=techo;

this->stateControl->setPiso(floor);
this->stateControl->setTecho(roof);
this->stateControl->asignar();

/*empaquetado vectorPiso= *this->stateControl->getVPiso();
empaquetado vectorTecho= *this->stateControl->getVTecho();
for (i=0;i< 32-contadorOV;i++) {
	this->vecotrPiso[0].b1 = vectorPiso[contadorOV-1].b1;
	this->vectorTecho[0].b1 = vectorTecho[contadorOV-1].b1;
}

for (i; i<32;i++) {
	this->vecotrPiso[0].b1 = 0;
	this->vectorTecho[0].b1 = 1;
}

this->floor= vectorToInt(&vectorPiso);
this->roof = vectorToInt(&vectorTecho);*/

}

void BitManager::updateAfterUF() {
	unsigned int i;
	unsigned int piso, bit0Piso;
	piso= this->stateControl->getPiso();
	unsigned int techo, bit0Techo;
	techo = this->stateControl->getTecho();
	unsigned int contadorUF;
	contadorUF= this->stateControl->getContadorUF();
	cout<<"contadorUF en update "<<contadorUF<<endl;
	unsigned int bit0;
	this->stateControl->mostrarBinario(piso,"pisoshiftantes");
	this->stateControl->mostrarBinario(techo,"techo shift antes");

	bit0 = ~0;
	bit0 = (bit0>>1) +1;

	if (contadorUF>0) {

		bit0Piso= piso & bit0;
		bit0Techo= techo & bit0;
		//piso= piso<<1;
		//techo = techo<<1;


		for (i=0;i<contadorUF+1;i++) {
			piso = (piso<<1) + 0;
			techo = (techo<<1) + 1;
			cout<<i<<" ";
			this->stateControl->mostrarBinario(piso,"pisoshift");
			this->stateControl->mostrarBinario(techo,"techo shift");
		}

		piso= piso>>1;
		techo = techo>>1;
		this->stateControl->mostrarBinario(piso,"pisoshift");
		this->stateControl->mostrarBinario(techo,"techo shift");
		piso = piso | 0;
		techo = techo | bit0;

		this->floor = piso;
		this->roof = techo;


		}



}

void BitManager::setCompressString(string compressString){

}

int BitManager::getIntToDescompress(){

	int resultado;


	return resultado;
}
